package com.library.dcy.servlet;

import com.alibaba.fastjson.JSON;
import com.library.dcy.dao.LibraryDaoImpl;
import com.library.dcy.entity.Library;
import com.library.dcy.service.LibraryServiceImpl;
import com.library.dcy.util.Page;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * servlet类
 */
public class LibraryServlet extends HttpServlet {
    /**
     * doGet方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        LibraryServiceImpl libraryService = new LibraryServiceImpl();
        LibraryDaoImpl libraryDao=new LibraryDaoImpl();
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || currentPage.equals("")) {
            currentPage = "1";
        }
        Integer currPage = Integer.parseInt(currentPage);
        String czm = req.getParameter("czm");//操作符
        if (czm == null || czm.equals("")) {
            czm = "sel";
        }
        if (czm.equals("sel")) {//查询
            String name = req.getParameter("name");//名称
            if (name == null || name.equals("")) {
                name = "";
            }
            req.setAttribute("name", name);
            Page<Library> page = libraryService.findByPage(name, currPage);
            req.setAttribute("page", page);
            req.getRequestDispatcher("dcy/jfz.jsp").forward(req, resp);
        } else if (czm.equals("cancel")) { //注销
            String ID = req.getParameter("workingID");
            boolean isDel = libraryService.findCancel(Integer.parseInt(ID));
            String s = JSON.toJSONString(isDel);
            PrintWriter printWriter = resp.getWriter();
            printWriter.print(s);
            printWriter.flush();
            printWriter.close();
            } else if (czm.equals("upd")) {//修改
                String id = req.getParameter("ids");
                String idCard = req.getParameter("idCards");
                String pwd = req.getParameter("pwds");
                String names = req.getParameter("namess");
                String age = req.getParameter("ages");
                String phone = req.getParameter("phones");
                String money = req.getParameter("moneys");
                Library library=new Library(Integer.parseInt(id),"idCard","pwd","names",Integer.parseInt(age),"phone",Double.parseDouble(money));
                boolean replace = libraryService.replace(library);
                if(replace) {
                    req.getRequestDispatcher("dcy/jfz.jsp").forward(req, resp);
                }else{
                    req.getRequestDispatcher("dcy/replace.jsp").forward(req, resp);
                }
                }else if(czm.equals("cs")){
                String reaid = req.getParameter("reaid");
                String idcard = req.getParameter("idcard");
                String pwd = req.getParameter("pwd");
                String names = req.getParameter("name");
                String age = req.getParameter("age");
                String phone = req.getParameter("phone");
                String price = req.getParameter("price");
                req.setAttribute("reaid",reaid);
                req.setAttribute("idcard",idcard);
                req.setAttribute("pwd",pwd);
                req.setAttribute("names",names);
                req.setAttribute("age",age);
                req.setAttribute("phone",phone);
                req.setAttribute("price",price);
                req.getRequestDispatcher("dcy/replace.jsp").forward(req,resp);
                }else if (czm.equals("chong")){//充值
                    String id=req.getParameter("id");
                    int coutn =Integer.parseInt(id);
                    req.setAttribute("id",coutn);
                    String price=req.getParameter("price");
                    req.getRequestDispatcher("dcy/cz.jsp").forward(req,resp);
                }else if (czm.equals("cz")){
                    Integer id=Integer.parseInt(req.getParameter("id"));
                    Double money=Double.parseDouble(req.getParameter("price"));
                    Library library=new Library(id,money);
                    int coutns=libraryDao.chongzhi(library);
                    if (coutns==1){
                    req.getRequestDispatcher("dcy/jfz.jsp").forward(req,resp);
                }
            }
        }

    /**
     * doPsot方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
        @Override
        protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }

